import axios from "axios";
import _ from "lodash";
import { consts } from "../../../tools/consts";
import { getApiData, axiosGet, axiosPost, axiosDelete, axiosPut, ROOT_URL, getNestApiData,ROOT_URL_TE } from "../../../tools/HttpClient";
import { getMergeType, MergeType } from "../../../tools/Tools";
import HeaderService from "../classes/HeaderService";
import { tableData } from "./tableData";
// import { useModel } from 'umi';

// const { initialState  } = useModel('@@initialState');

//import { hideColumns } from "../classes/ZLService";
// const apiUrl = "http://localhost:8103"; //java服务
const apiUrl = ROOT_URL;
const apiUrlTe = ROOT_URL_TE;

const dbname = window.sessionStorage.electronParams
    ? JSON.parse(window.sessionStorage.electronParams)?.params?.DBName
    : consts.defaultDbName; // "202205188806-KH202012975";;
// const dbname = "2022080811596-C00028";
export var BatchOperationType;
(function (BatchOperationType) {
    BatchOperationType["DATA_RECALCULATE"] = "dr";
    BatchOperationType["VERIFY_RECALCULATE"] = "vr";
    BatchOperationType["VERIFY_STATISTICS"] = "vs";
    BatchOperationType["DATA_VERIFY_RECALCULATE"] = "dvr";
})(BatchOperationType || (BatchOperationType = {}));

// 获取 pageType 的函数，需要在组件内部调用
const getPageType = () => {
    try {
        const electronParams = window.sessionStorage.electronParams;
        if (electronParams) {
            const params = JSON.parse(electronParams)?.params;
            return params?.pageType;
        }
        return null;
    } catch (error) {
        console.error('Error getting pageType:', error);
        return null;
    }
};

export const getBsdwxydms = () => {
    try {
        const electronParams = window.sessionStorage.electronParams;
        if (electronParams) {
            const params = JSON.parse(electronParams)?.params;
            return params?.bsdwxydms;
        }
        return null;
    } catch (error) {
        console.error('Error getting bsdwxydms:', error);
        return null;
    }
};

export const getMergeTypeLocal = () => {

    try {
        const electronParams = window.sessionStorage.electronParams;
        if (electronParams) {
            const params = JSON.parse(electronParams)?.params;
            return params?.mergeType;
        }
        return null;
    } catch (error) {
        console.error('Error getting bsdwxydms:', error);
        return null;
    }
};
/**
 * 根据字符串content解析公式
 * @param serverData
 */
export const parseFormulaByContentApi = async (serverData) => {
    try {
        const response = await axios.post(`${apiUrl}/cpasTableOperation/parseFormulaByContent`, {
            dbname,
            ...serverData,
        });
        return response.data;
    }
    catch (e) {
        throw e;
    }
};
/**
 * 解析自定义公式
 */
export const parseFormulaApi = async (serverData) => {
    try {
        const response = await axios.post(`${apiUrl}/cpasTableOperation/parseFormula`, {
            dbname,
            ...serverData,
        });
        return response.data;
    }
    catch (e) {
        throw e;
    }
};
/**
 * 保存设计表
 * @param serverData
 */
export const saveTableApi = async (serverData) => {
    try {
        const response = await axios.post(`${apiUrl}/cpasTableEngine/saveCpasTable`, {
            dbname,
            ...serverData,
        });
        return response.data;
    }
    catch (e) {
        throw e;
    }
};
/**
 * 获取审计年度
 */
export const getAuditYearApi = async () => {
    try {
        const res = await axios.get(`${apiUrl}/cpasTableEngine/getAuditYear`, {
            params: { dbname },
        });
        return res.data;
    }
    catch (e) {
        throw e;
    }
};
/**
 * 获取所有表名称
 */
export const getVerifyDataApi = async (tid) => {
    try {
        const res = await axios.get(`${apiUrl}/cpasTableEngine/getVerifyData`, {
            params: { dbname, tid },
        });
        return res.data;
    }
    catch (e) {
        throw e;
    }
};
/**
 * 保存勾稽校验公式
 * @param serverData
 */
export const saveVerifyData = async (tid, artParseResultList, artVerifyFormulaItemList) => {
    try {
        const response = await axios.post(`${apiUrl}/cpasTableEngine/saveVerifyData`, {
            dbname,
            tid,
            artParseResultList,
            artVerifyFormulaItemList,
        });
        return response.data;
    }
    catch (e) {
        throw e;
    }
};
/**
 * 删除勾稽校验公式
 * @param serverData
 */
export const removeVerifyDataById = async (verifyDataList, tid) => {
    try {
        const response = await axios.post(`${apiUrl}/cpasTableEngine/removeVerifyDataById`, {
            dbname,
            verifyDataList,
            tid,
        });
        return response.data;
    }
    catch (e) {
        throw e;
    }
};
/**
 * 删除表
 * @param tname
 */
export const removeCpasTableByName = async (tname, data_id, type) => {
    try {
        const response = await axios.post(`${apiUrl}/cpasTableEngine/removeCpasTableByName`, {
            dbname,
            tname,
            data_id,
            type,
        });
        return response.data;
    }
    catch (e) {
        throw e;
    }
};
/**
 * 获取所有表名称
 */
export const getAllDesignTableApi = async (type) => {
    try {
        const res = await axios.get(`${apiUrl}/cpasTableEngine/getAllCpasTable`, {
            params: { dbname, type },
        });
        return res.data;
    }
    catch (e) {
        throw e;
    }
};
/**
 * 获取所有表名称
 */
export const getCpasTableByName = async (tname, data_id, type) => {
    try {
        const res = await axios.get(`${apiUrl}/cpasTableEngine/getCpasTableByName`, {
            params: { dbname, tname, data_id, type },
        });
        return res.data;
    }
    catch (e) {
        throw e;
    }
};
/**
 * 获取斌哥数据源
 * @param tname ,data_id 日期
 */
export const getDisclosureApiData = async (tname, data_id) => {
    try {
        const response = await axios.post(`${apiUrl}/disclosure/getDisclosureApiData`, {
            dbName: dbname,
            name: tname,
            data_id,
        });
        return response.data;
    }
    catch (e) {
        throw e;
    }
};
export const getCpasTableDataByTzTypeAPI = async (param) => {
    try {
        const res = await axios.post(`${apiUrl}/cpasTableEngine/getCpasTableDataByTzType`, param);
        return res.data;
    }
    catch (e) {
        throw e;
    }
};
/**
 * 过滤运行时列
 */
const filterRunTimeColumn = async (iGridData) => {
    return await filterRuntimeColumnApi(iGridData);
};
export const filterRuntimeColumnApi = async (iGridData) => {
    // runTime只会存在顶层列,不需要递归.
    const headerService = new HeaderService(null);
    const rootHeaders = iGridData.header;
    const headerColumn = headerService.getHeaderColumn(rootHeaders);
    const runTimeKeys = headerColumn
        .filter((item) => {
        if (item.config.isRuntime) {
            return true;
        }
        return false;
    })
        .map((item) => {
        return item.key;
    });
    let filterIGridData = iGridData;
    filterIGridData = await removeColumnByGridDataApi({
        keys: runTimeKeys,
        iGridData: filterIGridData,
    });
    return filterIGridData;
};
/**
 * 数转服务接口
 * sourceData: 披露表原数据
 * zlTableConfig: zl模式下的配置
 * sourceFormulaData: 披露表带公式原数据
 * isReedTid: 是否需要处理tid true 需要 false 不需要
 * 返回数据类型: IGridData[] | IGridData 数组或者对象根据isMoreGrid决定
 */
export const getDsToZlData = async (displayIGridData, zlTableConfig, sourceFormulaData, tid, isReedTid) => {
    try {
        // 过滤运行时列
        // displayIGridData = await filterRunTimeColumn(displayIGridData);
        // sourceFormulaData = await filterRunTimeColumn(sourceFormulaData);
        const getCurrentMergeType = getMergeType();
        //合并系统合并层的法定和合并用需要做调整数
        if (getCurrentMergeType === MergeType.HB_HBYDT ||
            getCurrentMergeType === MergeType.HB_FD ||
            getCurrentMergeType === MergeType.DT_HBYDT) {
            const cDisplayData = displayIGridData.data;
            const iGridDataParam = {
                ...displayIGridData,
                data: sourceFormulaData.data,
                displayData: cDisplayData,
            };
            const iGridData1 = (await getCpasTableDataByTzTypeAPI({
                iGridData: iGridDataParam,
                tzType: "tzh",
            })).data;
            if (iGridData1?.displayData) {
                displayIGridData.data = iGridData1?.displayData;
                displayIGridData.header = iGridData1.header;
            }
            if (iGridData1?.data) {
                sourceFormulaData.data = iGridData1.data;
                sourceFormulaData.header = iGridData1.header;
            }
            // const res = await getDsToTZData(sourceFormulaData, tid);
            // if (res) {
            //   sourceFormulaData = res;
            //   //重算出调整后的显示值
            //   const resDisplayData = await reCalculateNote(res);
            //   if (resDisplayData?.data?.displayData) {
            //     sourceData = {
            //       ...sourceData,
            //       data: resDisplayData?.data?.displayData,
            //     };
            //   }
            //   // console.log(resDisplayData, "response----");
            // }
        }
        // 过滤运行时列
        displayIGridData = await filterRunTimeColumn(displayIGridData);
        sourceFormulaData = await filterRunTimeColumn(sourceFormulaData);
        const response = await axios.post(`${apiUrl}/cpasTableOperation/getDsToZlData`, {
            dbname,
            sourceData: displayIGridData,
            zlTableConfig,
            sourceFormulaData,
            tid: isReedTid ? tid : null,
        });
        return response.data;
    }
    catch (e) {
        throw e;
    }
};
/**
 * 附注调整数转服务接口
 * sourceData: 披露表原数据
 * tid: tid
 * 返回数据类型: IGridData[] | IGridData 数组或者对象根据isMoreGrid决定
 */
export const getDsToTZData = async (sourceData, tid, isRunColumn) => {
    // 过滤运行时列
    if (isRunColumn) {
        sourceData = await filterRunTimeColumn(sourceData);
    }
    try {
        const response = await axios.post(`${apiUrl}/cpasTableOperation/calculateAdjustedNote`, {
            dbname,
            sourceData,
            tid,
        });
        return response?.data?.data;
    }
    catch (e) {
        throw e;
    }
};
/**D
 * 根据表名称获取单元格数据
 * @param tid
 */
export const getCpasTableCells = async (name, data_id, type) => {
    try {
        const res = await axios.get(`${apiUrl}/cpasTableEngine/getCpasTableCells`, {
            params: { tname: name, dbname, data_id, type },
        });
        return res.data;
    }
    catch (e) {
        throw e;
    }
};
/**
 * 获取表数据根据tid
 * @param tid
 */
export const getDesignTableInfoByTidApi = async (tid, type, data_type) => {
    try {
        const res = await axios.get(`${apiUrl}/cpasTableEngine/getCpasTableByTid`, {
            // params: { tid: tid, dbname: "1-AUD-2023-003", type, data_type },//测试验证
            params: { tid: tid, dbname, type, data_type },
        });
        return res.data;
    }
    catch (e) {
        throw e;
    }
};
/**
 * 获取表数据根据tid
 * @param tid
 */
export const getCpasTableCellData = async (tname, rmap, cmap, type) => {
    try {
        const res = await axios.get(`${apiUrl}/cpasTableEngine/getCpasTableCellData`, {
            params: { tname, dbname, rmap, cmap, type },
        });
        return res.data;
    }
    catch (e) {
        throw e;
    }
};
export const syncLoad = () => {
    const xhr = new XMLHttpRequest();
    const testParams = "tname=%E6%9C%AA%E5%AE%A1%E8%B4%A2%E5%8A%A1%E6%95%B0%E6%8D%AE&dbname=FI-2017-00001&rmap=%E5%BA%94%E6%94%B6%E8%B4%A6%E6%AC%BE&cmap=QMYE";
    const url = `${apiUrl}/cpasTableEngine/getCpasTableCellData?${testParams}`;
    xhr.open("get", url, false); // 同步请求
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xhr.send();
    // debugger;
    if (xhr.status === 200) {
        const res = JSON.parse(xhr.responseText);
        return res.data;
    }
};
/**
 * 删除列
 */
export const removeColumnByGridDataApi = async (params) => {
    try {
        const response = await axios.post(`${apiUrl}/cpasTableEngine/removeColumnByGridData`, {
            dbname,
            ...params,
        });
        return response.data.data;
    }
    catch (e) {
        throw e;
    }
};
/**
 * 根据CpasTableJson获取入库数据
 */
export const getCpasTableServerDataByJsonApi = async (params) => {
    try {
        const response = await axios.post(`${apiUrl}/cpasTableEngine/getCpasTableServerDataByJson`, {
            dbname,
            ...params,
        });
        return response.data;
    }
    catch (e) {
        throw e;
    }
};
/**
 * 获取表数据根据name
 */
export const getDesignTableInfoByNameApi = async (name, data_id, type, grid_data_type, data_type, isNeedBeforeData) => {
    console.log("🚀 ~ getDesignTableInfoByNameApi ~ 函数开始执行", { name, data_id, type, grid_data_type, data_type, isNeedBeforeData });
    
    try {
        const pageType = getPageType();
        const bsdwxydms = getBsdwxydms(); 
        const mergeTypeLocal = getMergeTypeLocal(); 
        
        let res; // 声明变量
        
        if(pageType == 1){ 
            console.log("🚀 ~ getDesignTableInfoByNameApi ~ 进入pageType == 1分支");
            res = await axios.post(`${apiUrlTe}/report/getReport`, {
                tname: name,
                dbname,
                data_id: data_id,
                type,
                grid_data_type,
                data_type,
                isNeedBeforeData,
                bsdwxydms,
                sjxmbh:1,
                mergeType:mergeTypeLocal
            });
            let  parsedData
            if(res.data.data){
                parsedData = JSON.parse(res.data.data.reportinfo); 
            }else{
                parsedData = tableData; 
            } 
            const result = { // 模拟下面本地接口返回的数据格式
                code:200,
                data:parsedData,
                msg:"success",
                ok:true 
            }
            return result;
        } else { 
            res = await axios.get(`${apiUrl}/cpasTableEngine/getCpasTableDataByName`, {
                params: {
                    tname: name,
                    dbname,
                    data_id: data_id,
                    type,
                    grid_data_type,
                    data_type,
                    isNeedBeforeData,
                },
            });
            console.log("🚀 ~ getDesignTableInfoByNameApi ~ else分支返回数据:", res.data);
            return res.data;
        }
    }
    catch (e) {
        console.error("🚀 ~ getDesignTableInfoByNameApi ~ 捕获到错误:", e);
        throw e;
    }
};;
/**
 * 获取基础报表数据列映射
 */
export const getColumnsMapByReport = async () => {
    try {
        const res = await axios.get(`${apiUrl}/cpasTableEngine/getColumnsMapByReport`, {
            params: { dbname },
        });
        return res.data;
    }
    catch (e) {
        throw e;
    }
};
/**
 * 获取报表基础数据行映射
 * @param tid
 */
export const getRowsMapByReport = async () => {
    try {
        const res = await axios.get(`${apiUrl}/cpasTableEngine/getRowsMapByReport`, {
            params: { dbname },
        });
        return res.data;
    }
    catch (e) {
        throw e;
    }
};
/**
 * out:取数公式 -  TODO 暂时未移植到nodeJs中
 * @param params
 */
export const getOutTableParamsApi = async (params) => {
    try {
        const response = await axios.post(`${apiUrl}/vtable/getOutTableParams`, {
            ...params,
        });
        return response.data.data;
    }
    catch (e) {
        throw e;
    }
};
/**
 * 勾稽校验报告基础数据
 * @param params
 * @returns
 */
export const getBaseDataApi = async (url, params) => {
    try {
        const response = await axios.post(`${apiUrl}/sqlapis/getApiData`, {
            name: url,
            params: {
                dbname,
                ...params,
            },
        });
        return response.data.data;
    }
    catch (e) {
        throw e;
    }
};
/**
 *
 * @param params 勾稽校验报告基础数据
 * @returns
 */
export const getAnnotatedDataApi = async (dataId) => {
    try {
        const response = await axios.get(`${apiUrl}/disclosure/notes`, {
            params: {
                dbName: dbname,
                dataId: dataId,
            },
        });
        return response.data.data;
    }
    catch (e) {
        throw e;
    }
};
/**
 *
 * @param params 获取基础数据
 * @returns
 */
export const getDisclosureData = async (param) => {
    try {
        const response = await axios.post(`${apiUrl}/disclosure/getDisclosureApiData`, {
            dbName: dbname,
            ...param,
        });
        return response.data;
    }
    catch (e) {
        throw e;
    }
};
/**
 *
 * @param params 附注数据根据id获取数据
 * @returns
 */
export const getCpasTableList = async (param) => {
    try {
        const response = await axios.get(`${apiUrl}/cpasTableEngine/getCpasTableByTid`, {
            params: { dbname, ...param },
        });
        return response.data;
    }
    catch (e) {
        throw e;
    }
};
// 删除勾稽校验公式
export const removeVerifyData = async (tid) => {
    try {
        const response = await axios.post(`${apiUrl}/cpasTableEngine/removeVerifyDataByTid`, {
            dbname,
            tid,
        });
        return response.data;
    }
    catch (e) {
        throw e;
    }
};
/**
 * 期间类型运算
 */
export const periodTypeData = async (param) => {
    try {
        const response = await axios.post(`${apiUrl}/cpasTableOperation/periodTypeData`, {
            dbname,
            ...param,
        });
        return response.data;
    }
    catch (e) {
        throw e;
    }
};
/**
 * 动态行列运算
 */
export const parseLogicFormulaData = async (action, iGridData, tid) => {
    try {
        const response = await axios.post(`${apiUrl}/cpasTableOperation/parseLogicFormulaData`, {
            dbname,
            action,
            iGridData,
            tid,
        });
        return response.data;
    }
    catch (e) {
        throw e;
    }
};
/**
 * 刷新所有聚合行列所有公式: iGridData和 tid必须有一个参数
 */
export const refreshAllTagFormulasAPI = async (iGridData, tid) => {
    try {
        const response = await axios.post(`${apiUrl}/cpasTableOperation/refreshAllTagFormulas`, {
            dbname,
            iGridData,
            tid,
        });
        return response.data;
    }
    catch (e) {
        throw e;
    }
};
export const clearCpasTableData = async (iGridData, dValueData) => {
    try {
        const response = await axios.post(`${apiUrl}/cpasTableOperation/clearCpasTableData`, {
            dbname,
            iGridData,
            dValueData,
            isUpdateData: false,
        });
        return response.data;
    }
    catch (e) {
        throw e;
    }
};
export const clearCpasTableFormula = async (iGridData, dValueData) => {
    try {
        const response = await axios.post(`${apiUrl}/cpasTableOperation/clearCpasTableFormula`, {
            iGridData,
            dValueData,
        });
        return response.data;
    }
    catch (e) {
        throw e;
    }
};
/**
 * 刷新聚合列公式返回: iGridData 和 tid必须有一个参数
 */
export const refreshPolymericColumnFormulaAPI = async (cols, iGridData, tid) => {
    try {
        const response = await axios.post(`${apiUrl}/cpasTableOperation/refreshPolymericColumnFormula`, {
            dbname,
            iGridData,
            tid,
            cols,
        });
        return response.data;
    }
    catch (e) {
        throw e;
    }
};
/**
 * 刷新聚合列公式返回: iGridData 和 tid必须有一个参数
 */
export const refreshPolymericRowFormulaAPI = async (rows, iGridData, tid) => {
    try {
        const response = await axios.post(`${apiUrl}/cpasTableOperation/refreshPolymericRowFormula`, {
            dbname,
            tid,
            rows,
            iGridData: _.cloneDeep(iGridData),
        });
        return response.data;
    }
    catch (e) {
        throw e;
    }
};
export const reCalculateNote = async (iGridData) => {
    try {
        const response = await axios.post(`${apiUrl}/cpasTableOperation/dataRecalculate`, {
            dbname,
            iGridData,
        });
        return response.data;
    }
    catch (e) {
        throw e;
    }
};
/**
 * 根据勾稽公式进行运算。
 */
export const getVerifyDataByFormula = async (sourceData, displayData, formulas, tid, type, headerColumn) => {
    try {
        const response = await axios.post(`${apiUrl}/cpasTableEngine/getVerifyDataByFormula`, {
            dbname,
            sourceData,
            displayData,
            formulas,
            tid,
            cpasType: type && type === "zl" ? "ds" : type,
            headerColumn,
        });
        return response.data;
    }
    catch (e) {
        throw e;
    }
};
/**
 * 生成勾稽公式进行运算。
 */
export const generateVerifyDataByFormula = async (formulas, tid, formatInfo) => {
    try {
        const response = await axios.post(`${apiUrl}/cpasTableOperation/reCalculateDynamicVerify`, {
            dbname,
            formulas,
            tid,
            formatInfo,
        });
        return response.data;
    }
    catch (e) {
        throw e;
    }
};
/**
 * 解析校验公式
 */
export const parseVerifyFormat = async (params) => {
    try {
        const response = await axios.post(`${apiUrl}/cpasTableOperation/parseVerifyFormat`, {
            dbname,
            ...params,
        });
        return response.data;
    }
    catch (e) {
        throw e;
    }
};
export async function getCheckTypeApi(name, params) {
    const res = await getApiData(name, {
        ...params,
        dbName: dbname,
    });
    if (res.ok) {
        return res.data;
    }
    return {};
}
//左侧树获取树节点列表  GET /disclosure/notes
export async function getLeftTreeList(dataId) {
    const params = {
        dbName: dbname,
        dataId: dataId,
    };
    try {
        const res = await axiosGet("/disclosure/notes", {
            dbname,
            ...params,
        }, {
            headers: {
                "Content-Type": "application/json",
            },
        });
        return res?.data?.data;
    }
    catch (error) {
        return Promise.resolve({ data: error, ok: false, code: 100000 });
    }
}
// 根据tids查询cpastable数据
export async function getCpasTableByTids(params) {
    try {
        const response = await axiosPost("/cpasTableEngine/getCpasTableByTids", {
            dbname,
            ...params,
        });
        return response.data;
    }
    catch (error) {
        return Promise.resolve({ data: error, ok: false, code: 100000 });
    }
}
// excel批量导入且执行列匹配操作
export async function importExcelToColumnMatchAPI(params) {
    try {
        const response = await axiosPost("/cpasTableEngine/importExcelToColumnMatch", {
            dbname,
            ...params,
        });
        return response?.data?.data;
    }
    catch (error) {
        throw error;
    }
}
// 保存excel批量导入数据入库
export async function saveImportExcelAPI(params) {
    try {
        const response = await axiosPost("/cpasTableEngine/saveExcelImport", {
            dbname,
            ...params,
        });
        return response?.data?.data;
    }
    catch (error) {
        throw error;
    }
}
/**自动匹配excel列和系统列,返回双方列的对应关系
 *
 * @param params
 * @returns
 */
export async function autoMathExcelToSystemAPI(params) {
    try {
        const response = await axiosPost("/cpasTableEngine/autoMathExcelToSystem", {
            dbname,
            ...params,
        });
        return response?.data?.data;
    }
    catch (error) {
        throw error;
    }
}
/*从外部模板文件中查询NotesTree列表
GET /template/getNotesTreeListNew
接口ID：99961872
接口地址：https://app.apifox.com/project/1267497/apis/api-99961872*/
export async function getTemplateForNotesTreeList(params) {
    try {
        const res = await axiosGet("/template/getNotesTreeListNew", {
            dbname,
            ...params,
        });
        return res.data;
    }
    catch (error) {
        return Promise.resolve({ data: error, ok: false, code: 100000 });
    }
}
// 导入模版
export async function menuTemplateExport(params) {
    try {
        const res = await axiosPost("/template/exportData", {
            dbName: dbname,
            ...params,
        });
        return res.data;
    }
    catch (error) {
        return Promise.resolve({ data: error, ok: false, code: 100000 });
    }
}
// 查询任意文件夹下有没有存在的文件
export async function menuExportHaveTemplate(params) {
    try {
        const res = await axiosGet("/template/queryAnyFileIfExistInDisk", {
            ...params,
        });
        return res.data;
    }
    catch (error) {
        return Promise.resolve({ data: error, ok: false, code: 100000 });
    }
}
/**
 *
 * @param params 导入db
 * @returns
 */
export async function menuTemplateMerge(params) {
    try {
        const res = await axiosPost("/template/importTemplate", {
            dbName: dbname,
            ...params,
        });
        return res.data;
    }
    catch (error) {
        return Promise.resolve({ data: error, ok: false, code: 100000 });
    }
}
// 查询cpastable表
export async function getCpasTableName(params) {
    try {
        const res = await axiosGet("/cpasTableEngine/getCpasTableByName", {
            dbname,
            ...params,
        });
        return res.data;
    }
    catch (error) {
        return Promise.resolve({ data: error, ok: false, code: 100000 });
    }
}
/**
 * 批量钩稽重算
 * @param params
 * @returns
 */
export async function getAllVerifyTypeData(params) {
    try {
        const res = await axiosPost("/cpasTableOperation/batchVerifyRecalculate", {
            ...params,
            dbname,
        });
        return res.data;
    }
    catch (error) {
        return Promise.resolve({ data: error, ok: false, code: 100000 });
    }
}
// 根据附注顶级节点及子节点查询所在整理模式的数据并批量导出excel
export async function getNotesTreeZLToExportExcel(params) {
    try {
        const res = await axiosPost("/cpasTableEngine/batchExportExcelResult", {
            dbname,
            ...params,
        });
        const response = res.data;
        const tempMsg = "EBUSY: resource busy or locked, open";
        const tempMsg2 = "ENOENT";
        const isTrue = response.msg.includes(tempMsg) || response.msg.includes(tempMsg2);
        if (isTrue) {
            return {
                code: response.code,
                msg: "文件已被程序打开，需要关闭已被打开的文件。",
                ok: response.ok,
                type: "导出",
            };
        }
        return response;
    }
    catch (error) {
        return Promise.resolve({ data: error, ok: false, code: 100000 });
    }
}
// 批量导出财务报表
export async function getCpatableToBatchExportExcel(params) {
    try {
        const res = await axiosPost("/cpasTableEngine/getCpatableToBatchExportExcel", {
            dbname,
            ...params,
        });
        console.log(res, "res----");
        const response = res.data;
        const tempMsg = "EBUSY: resource busy or locked, open";
        const tempMsg2 = "ENOENT";
        const isTrue = response.msg.includes(tempMsg) || response.msg.includes(tempMsg2);
        if (isTrue) {
            return {
                code: response.code,
                msg: "文件已被程序打开，需要关闭已被打开的文件。",
                ok: response.ok,
                type: "导出",
            };
        }
        return response;
    }
    catch (error) {
        return Promise.resolve({ data: error, ok: false, code: 100000 });
    }
}
export async function addTreeNodePage(params) {
    try {
        const res = await axiosPost("/disclosure/notes", {
            dbname,
            ...params,
        });
        return res.data;
    }
    catch (error) {
        return Promise.resolve({ data: error, ok: false, code: 100000 });
    }
}
export async function delTreeNodePage(id, dbName) {
    const params = {
        dbname,
        dbName: dbName,
    };
    try {
        const res = await axiosDelete(`/disclosure/notes/` + id, params, {
            "Content-Type": "application/json",
        });
        return res.data;
    }
    catch (error) {
        return Promise.resolve({ data: error, ok: false, code: 100000 });
    }
}
export async function setEditTreeTittle(params) {
    try {
        const res = await axiosPut(`/disclosure/notes/${params.id}`, params, {
            "Content-Type": "application/json",
        });
        return res.data;
    }
    catch (error) {
        return Promise.resolve({ data: error, ok: false, code: 100000 });
    }
}
export async function moveTreeNodePage(params, id) {
    try {
        const res = await axiosPut(`/disclosure/notes/adjust/${id}`, params, {
            "Content-Type": "application/json",
        });
        return res.data;
    }
    catch (error) {
        return Promise.resolve({ data: error, ok: false, code: 100000 });
    }
}
export async function fetchReportList(params) {
    try {
        const res = await axiosGet(`/disclosure/notes/report-items`, params);
        return res.data;
    }
    catch (error) {
        return Promise.resolve({ data: error, ok: false, code: 100000 });
    }
}
export async function editCpasTableNames(params) {
    try {
        const res = await axiosPost(`/cpasTableEngine/renameCpasTable`, params);
        return res.data;
    }
    catch (error) {
        return Promise.resolve({ data: error, ok: false, code: 100000 });
    }
}
export async function copyCpasTableFun(params) {
    try {
        const res = await axiosGet(`/cpasTableEngine/getCpasTableByTid`, params);
        return res.data;
    }
    catch (error) {
        return Promise.resolve({ data: error, ok: false, code: 100000 });
    }
}
export async function clearDataAll(params) {
    try {
        const res = await axiosPost(`/cpasTableOperation/batchClearCpasTableData`, params);
        return res.data;
    }
    catch (error) {
        return Promise.resolve({ data: error, ok: false, code: 100000 });
    }
}
export async function queryDataState(params) {
    try {
        const res = await axiosGet(`/cpasTableOperation/getOperationState`, params);
        return res.data;
    }
    catch (error) {
        return Promise.resolve({ data: error, ok: false, code: 100000 });
    }
}
export async function queryResetState(params) {
    try {
        const res = await axiosPost(`/cpasTableOperation/updateOperationState`, params);
        return res.data;
    }
    catch (error) {
        return Promise.resolve({ data: error, ok: false, code: 100000 });
    }
}
export async function queryVerifyStatezl(params) {
    try {
        const res = await axiosGet(`/cpasTableEngine/getVerifyResult`, params);
        return res.data;
    }
    catch (error) {
        return Promise.resolve({ data: error, ok: false, code: 100000 });
    }
}
export async function fetchAlwaysShow(params) {
    try {
        const res = await axiosPost(`/disclosure/always-show`, params);
        return res.data;
    }
    catch (error) {
        return Promise.resolve({ data: error, ok: false, code: 100000 });
    }
}
export async function getTemplateExportToSqlite(params) {
    try {
        const res = await axiosPost(`/template/exportDataToSqlite`, params);
        return res.data;
    }
    catch (error) {
        return Promise.resolve({ data: error, ok: false, code: 100000 });
    }
}
export async function getNotesIsEmptyAPI(params) {
    try {
        const res = await axiosGet(`/disclosure/notes-is-empty`, params, {
            "Content-Type": "application/json",
        });
        return res.data;
    }
    catch (error) {
        return Promise.resolve({ data: error, ok: false, code: 100000 });
    }
}
export async function clearFormulaAPI(params) {
    try {
        const res = await axiosPost(`/cpasTableOperation/batchClearFormulas`, params);
        console.log(res);
        return res.data;
    }
    catch (error) {
        return Promise.resolve({ data: error, ok: false, code: 100000 });
    }
}
/**
 * 修复design_column cmap字段(保存模板之前需要调用修复库中的错误数据)
 */
export const fixDesignColumnCmapAPI = async (type) => {
    try {
        const res = await axios.get(`${apiUrl}/FixPatch/fixDesignColumnCmap`, {
            params: {
                dbname,
                type,
            },
        });
        return res.data;
    }
    catch (e) {
        throw e;
    }
};
export async function reCalculateNotesAPI(params) {
    try {
        const res = await axiosPost(`/cpasTableOperation/batchDataAndVerifyRecalculate`, {
            ...params,
            type: BatchOperationType.DATA_VERIFY_RECALCULATE,
        });
        console.log(res);
        return res.data;
    }
    catch (error) {
        return Promise.resolve({ data: error, ok: false, code: 100000 });
    }
}
export async function menuTemplateBatch(params) {
    try {
        const res = await axiosPost(`/cpasTableOperation/batchOpenOrCloseFormula`, params);
        return res.data;
    }
    catch (error) {
        return Promise.resolve({ data: error, ok: false, code: 100000 });
    }
}
export async function getSeekAuditReportTemplate(params) {
    try {
        const res = await getApiData("account.seekAuditReportTemplate", params);
        if (res.ok) {
            return Promise.resolve({
                data: res.data,
                success: true,
            });
        }
        return Promise.resolve({
            data: "接口account.seekAuditReportTemplate出错了！",
            success: false,
        });
    }
    catch (error) {
        return Promise.resolve({
            data: "接口account.seekAuditReportTemplate出错了！",
            success: false,
        });
    }
}
export async function templateInitApi(params) {
    try {
        const res = await getNestApiData("/template/importTemplate", "post", params);
        if (res.ok) {
            return Promise.resolve({
                data: res.ok,
                msg: res.msg,
                success: true,
            });
        }
        return Promise.resolve({
            data: res.ok,
            msg: res.msg,
            success: false,
        });
    }
    catch (error) {
        return Promise.resolve({
            data: "接口/template/importTemplate,请求失败！",
            success: false,
        });
    }
}
export async function getReportItemsZero(params) {
    const res = await axiosGet(`/disclosure/report-items-zero`, {
        ...params,
        dbName: dbname,
    });
    return res.data;
}
export async function getTemplateInfo(zcbfid) {
    try {
        const { data } = await axiosPost(`/sqlapis/getApiData?api=account.seekAuditReportTemplate`, {
            name: "account.seekAuditReportTemplate",
            params: {
                zcbfid: zcbfid,
            },
        });
        return data;
    }
    catch (e) {
        return Promise.resolve({ data: e, ok: false, code: 100000 });
    }
}
/**
 * 保存设计表
 * @param dbname
 * @param templateName
 * @param tableNames
 * @param type
 * @param dateId
 * @param action
 * @param mode
 * @param tids
 */
export const singleDSTableInit = async (dbname, templateName, tableNames, type, dateId) => {
    try {
        const response = await axiosPost(`/template/importTemplate`, {
            dbName: dbname,
            type,
            templateName,
            tableNames,
            dataId: dateId,
            action: "initOne",
            opt: {
                duplicate: "cover",
            },
        });
        return response.data;
    }
    catch (e) {
        throw e;
    }
};
/**
 * 从模板中查询数据
 */
export const queryCpasTableByMb = async (dbname, templateName, tableNames, type) => {
    try {
        const response = await axiosPost(`/template/getTemplateTablesInfo`, {
            dbname,
            type,
            templateName,
            tableNames,
        });
        return response.data;
    }
    catch (e) {
        throw e;
    }
};
/**
 * 获取财务报表表名
 * @param tid
 */
export const getAllCpasTable = async (params) => {
    try {
        const resonse = await axiosGet("/cpasTableEngine/getAllCpasTable", params);
        const res = resonse.data;
        if (res.ok) {
            return Promise.resolve({
                data: res.data,
                success: true,
            });
        }
        return Promise.resolve({
            data: res.data,
            success: false,
        });
    }
    catch (error) {
        return Promise.resolve({
            data: "接口getAllCpasTable出错了！",
            success: false,
        });
    }
};
export default {
    saveTableApi,
    getAllDesignTableApi,
    getDesignTableInfoByTidApi,
    getBaseDataApi,
    getAnnotatedDataApi,
    getDisclosureData,
    getCpasTableList,
    refreshAllTagFormulasAPI,
    refreshPolymericColumnFormulaAPI,
    refreshPolymericRowFormulaAPI,
    getCheckTypeApi,
    getBsdwxydms,
};
